﻿namespace App

open System
open System.Windows
open System.Windows.Controls
open System.Windows.Markup
open System.Windows.Threading 
            
module App =

    type App() as self =
        inherit Application()

        let mainWindow = new MainWindow()        
        
        let handleUnhandledException(e : DispatcherUnhandledExceptionEventArgs) =             
            (*  Your centralized exception handling here. But not all unhandled exception raise DispatcherUnhandledException event.
                [MSDN ref: http://msdn.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx]
                If an exception is not handled on either a background user interface (UI) thread (a thread with its own Dispatcher) or 
                a background worker thread (a thread without a Dispatcher), 
                the exception is not forwarded to the main UI thread. Consequently, DispatcherUnhandledException is not raised. 
                In these circumstances, you will need to write code to do the following:    
                Handle exceptions on the background thread.Dispatch those exceptions to the main UI thread.
                Rethrow them on the main UI thread without handling them to allow DispatcherUnhandledException to be raised.
             *)
             MessageBox.Show(e.Exception.Message) |> ignore (* This is just sample. You may need to add your own exception handling mechanism. *)
             e.Handled <- true
        
        do
            let dictionary = new ResourceDictionary()
            dictionary.Source <- new Uri("/App;component/Themes/ExpressionDark.xaml",  UriKind.RelativeOrAbsolute);
            self.Resources.MergedDictionaries.Add(dictionary)   

        member self.RunApp() =
            self.DispatcherUnhandledException.Add(handleUnhandledException)
            self.Run(mainWindow)   
                            
    [<STAThread; EntryPoint>]    
    let main _ =         
        let app = new App()
        app.RunApp()
        
        